<!--微信账号-->
<template>
  <div class="app-container">
    <div class="search">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="68px">
      <el-input
        class="search-input"
        v-model="queryParams.wxName"
        placeholder="账号名称"></el-input>

      <el-button type="primary" icon="el-icon-search" size="mini" @click="getPlayerWxTransactions" style="margin-left: 20px;">筛选</el-button>
      <el-button type="primary" size="mini" @click="getReviewTransactions">待审核</el-button>
      <el-button type="primary" size="mini" @click="getAllTransactions">全部</el-button>
      </el-form>
    </div>
    <h1>充值账单</h1>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        label="账号名称"
        align="center"
        prop="wxName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="交易类型"
        align="center"
        prop="transactionType"
        :show-overflow-tooltip="true"
        :formatter="transactionTypeFormatter"
      />
      <el-table-column
        label="金额"
        align="center"
        prop="amount"
        :show-overflow-tooltip="true"
        :formatter="formatAmount"
      />
      <el-table-column
        label="余额"
        align="center"
        prop="balance"
        :show-overflow-tooltip="true"
        :formatter="formatAmount"
      />
      <el-table-column
        label="交易申请时间"
        align="center"
        prop="transactionDisplayTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" >
       <template slot="header" scope="scope">操作</template>
        <template slot-scope="scope">
          <!-- 根据 status 值显示不同的内容 -->
          <div v-if="scope.row.status === 1">
            已审核通过
          </div>
          <div v-if="scope.row.status === -1" style="color: gray; text-decoration: line-through;">
            已取消充值
          </div>
          <div v-else-if="scope.row.status === 0">
            <el-button
              size="mini"
              type="text"
              @click="goClick(1, scope.row)">审核通过</el-button>
            <el-button
              size="mini"
              type="text"
              style="color: red;"
              @click="goClick(2, scope.row)">取消充值</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTransactions"
    />
  </div>
</template>
<script>
import {
  searchTransactions,
  confirmTransaction,
  rejectTransaction,
} from "@/api/data/transaction.js";
import {
  goSearchWeChat,
} from "@/api/account/wx.js";
export default {
  data() {
    return {
      queryParams: {
        wxName: undefined,
        status: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      title: "提现申请",
      loading: false,
      tableData: [],
      total: 0,
      options: [],
    };
  },
  mounted() {
    this.getAllTransactions();
  },
  methods: {
    // 查询微信账号列表
    getAllTransactions() {
      this.queryParams.wxName = undefined;
      this.queryParams.status = undefined;

      this.getTransactions();
    },

    getPlayerWxTransactions(){
      this.queryParams.status = undefined;
      this.getTransactions();
    },

    getReviewTransactions(){
       this.queryParams.status = 0;
       this.getTransactions();
    },

    getTransactions() {
      this.loading = true;
      searchTransactions(this.queryParams).then((res) => {
        console.info("searchTransactions info:", this.tableData);
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch((error) => {
        // 处理接口异常
        console.error("searchTransactions error:", error);
        this.loading = false;
      });
    },

    transactionTypeFormatter(row, column) {
      const transactionType = row[column.property];
      if (transactionType == 1) {
        return '充值';
      }
      if (transactionType == 2) {
        return '提现';
      }
      return '未知' ;
    },

    // 操作
    goClick(type, row) {
      switch (type) {
        case 0:
          this.dialogVisible = true;
          break;
        case 1:
          this.form = { ...row };
          confirmTransaction(this.form).then((res) => {
            console.info("confirmTransaction info:", this.tableData);
            // -> 刷新表单
            this.getTransactions();
          }).catch((error) => {
            // 处理接口异常
            console.error("confirmTransaction error:", error);
          });
          break;
        case 2:
          this.form = { ...row };
          let info = `取消 ${this.form.wxName} 的 $${this.form.amount.toFixed(2)} 充值`;
          console.info("goClick 取消 :", info);
          this.$alert(info, '是否确定取消充值', {
              confirmButtonText: '确定',
              callback: action => {
                // 用户确认后的回调, 取消充值
                rejectTransaction(this.form).then((res) => {
                    console.info("rejectTransaction success:");
                    // -> 刷新表单
                    this.getTransactions();
                 }).catch((error) => {
                    // 处理接口异常
                    console.error("rejectTransaction error:", error);
                 });
              }
            });
          break;
      }
    },

    // 跳转微信详情
    goWxInfo(row) {
      this.$router.push({
        path: "/player/wxDetail",
        query: { id: row.id },
      });
    },

    formatAmount(row, column, cellValue) {
        return cellValue.toFixed(2);
    },
  },
};
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  .search-input {
    width: 240px;
  }
}
</style>
